package com.tomorrow.runner.data;

import android.content.ContentProvider;
import android.content.ContentProviderOperation;
import android.content.ContentProviderResult;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.OperationApplicationException;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.net.Uri;
import android.os.Bundle;
import android.util.SparseArray;

import java.util.ArrayList;

/**
 * 
 * cn.sucun.android.server.AbsSubProvider
 * 
 * Instance DIR_BASE_TYPE vnd.android.cursor.dir/{authority} <br>
 * ITEM_BASE_TYPE vnd.android.cursor.item/{authority}
 * 
 * @author Jiangchangzheng <br>
 * 
 * @param <T>
 *            create at 2014-8-13 11:05:51
 */
public abstract class AbsSubProvider<T extends ContentProvider> {
	protected final String DIR_BASE_TYPE;
	protected final String ITEM_BASE_TYPE;

	protected final T mParent;
	protected final Context mContext;
	protected final ContentResolver mResolver;
	protected final String mAuthority;

	public AbsSubProvider(T parent, String authority) {
		mParent = parent;
		mContext = parent.getContext();
		mResolver = mContext.getContentResolver();
		mAuthority = authority;

		DIR_BASE_TYPE = ContentResolver.CURSOR_DIR_BASE_TYPE + "/" + authority;
		ITEM_BASE_TYPE = ContentResolver.CURSOR_ITEM_BASE_TYPE + "/"
				+ authority;
	}

	protected abstract int registerMatch(UriMatcher matcher, int offset);

	protected abstract void registerSQLiteOpenHelper(
			SparseArray<SQLiteOpenHelper> openHelpers);

	protected String[] getCallMatched() {
		return null;
	}

	protected boolean isSupportBulkInsert(int match) {
		return false;
	}

	protected int[] getMatchForApplyBatch() {
		return null;
	}

	protected void onCreate() {
	}

	protected void shutdown() {
	}

	public abstract String getType(Uri uri, int match);

	public abstract Cursor query(SQLiteDatabase db, int match, Uri uri,
								 String[] projection, String selection, String[] selectionArgs,
								 String sortOrder);

	public abstract Uri insert(SQLiteDatabase db, int match, Uri uri,
							   ContentValues values);

	public abstract int update(SQLiteDatabase db, int match, Uri uri,
							   ContentValues values, String selection, String[] selectionArgs);

	public abstract int delete(SQLiteDatabase db, int match, Uri uri,
							   String selection, String[] selectionArgs);

	public int bulkInsert(SQLiteDatabase db, int match, Uri uri,
						  ContentValues[] values) {
		throw new UnsupportedOperationException(getClass().getSimpleName()
				+ " not support bulkInsert");
	}

	public ContentProviderResult[] applyBatch(SQLiteDatabase db, int match,
											  ArrayList<ContentProviderOperation> opers)
			throws OperationApplicationException {
		throw new UnsupportedOperationException(getClass().getSimpleName()
				+ " not support applyBatch");
	}

	public Bundle call(String method, String arg, Bundle extras) {
		return null;
	}

}
